package me.zhengjie.modules.rest;

import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.domain.EnterpriseDic;
import me.zhengjie.modules.domain.EnterpriseDicRelation;
import me.zhengjie.modules.service.EnterpriseDicService;
import me.zhengjie.modules.utils.Contents;
import me.zhengjie.modules.utils.ReturnObject;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController()
@RequiredArgsConstructor
@RequestMapping("/dic")
public class EnterpriseDicController {

    private final EnterpriseDicService enterpriseDicService;

    @PostMapping("/add")
    public ReturnObject addDic(@RequestBody EnterpriseDic dic){
        ReturnObject returnObject=new ReturnObject();
        returnObject.setCode(Contents.RETURN_CODE_FAIL);
        dic.setDeleteFlag("0");
        try {
            int result=enterpriseDicService.AddDic(dic);
            if(result>=1){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setMessage(Contents.RETURN_MESSAGE_FAIL);
            }
        }catch (Exception e){
            returnObject.setMessage(Contents.RETURN_MESSAGE_EXCEPTION);
        }
        return returnObject;
    }

    @GetMapping("/findDicByPage")
    public Map<String, Object> findDicByPage(String name,Integer pageNo, Integer pageSize){
        Map<String, Object> map=new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("name",name);
        List<EnterpriseDic> list=enterpriseDicService.findDicByPage(map);
        int total=enterpriseDicService.findDicForPageByTotal(map);
        map=new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @PostMapping("/removeDic")
    public ReturnObject removeDic(@RequestBody EnterpriseDic dic){
        ReturnObject returnObject=new ReturnObject();
        returnObject.setCode(Contents.RETURN_CODE_FAIL);
        try {
            int result=enterpriseDicService.removeDic(dic);
            if(result>=1){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setMessage(Contents.RETURN_MESSAGE_FAIL);
            }
        }catch (Exception e){
            returnObject.setMessage(Contents.RETURN_MESSAGE_EXCEPTION);
        }
        return returnObject;
    }

    @PostMapping("/editDic")
    public ReturnObject editDic(@RequestBody EnterpriseDic dic){
        ReturnObject returnObject=new ReturnObject();
        returnObject.setCode(Contents.RETURN_CODE_FAIL);
        try {
            int result=enterpriseDicService.editDic(dic);
            if(result>=1){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setMessage(Contents.RETURN_MESSAGE_FAIL);
            }
        }catch (Exception e){
            returnObject.setMessage(Contents.RETURN_MESSAGE_EXCEPTION);
        }
        return returnObject;
    }

    @AnonymousAccess
    @GetMapping("/getDic")
    public ReturnObject findDicByType(){
        ReturnObject returnObject=new ReturnObject();
        Map<String, Object> map=new HashMap<>();
        map.put("honorList",enterpriseDicService.findDicByType("科技荣誉"));
        map.put("valueList",enterpriseDicService.findDicByType("企业价值"));
        map.put("standardList",enterpriseDicService.findDicByType("标准制定"));
        returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
        returnObject.setResultData(map);
        return returnObject;
    }

    @AnonymousAccess
    @GetMapping("/getDicCount")
    public ReturnObject findDicByValueToCount(){
        ReturnObject returnObject=new ReturnObject();
        Map<String, Object> map=new HashMap<>();
        map.put("h1",enterpriseDicService.findDicByValue("大优强企业"));
        map.put("h2",enterpriseDicService.findDicByValue("国家级单项冠军"));
        map.put("h3",enterpriseDicService.findDicByValue("国家级专精特新小巨人"));
        map.put("h4",enterpriseDicService.findDicByValue("放水养鱼"));
        map.put("h5",enterpriseDicService.findDicByValue("高技术企业"));
        map.put("h6",enterpriseDicService.findDicByValue("数字经济核心产业"));
        map.put("h7",enterpriseDicService.findDicByValue("创新型初创企业"));
        map.put("h8",enterpriseDicService.findDicByValue("科技型中小企业"));
        map.put("h9",enterpriseDicService.findDicByValue("A级纳税人信用评级"));
        returnObject.setResultData(map);
        return returnObject;
    }

    @PostMapping("/relation")
    public ReturnObject relationDicAndEnter(@RequestBody EnterpriseDicRelation relation){
        System.out.println(relation);
        ReturnObject returnObject=new ReturnObject();
        returnObject.setCode(Contents.RETURN_CODE_FAIL);
        try {
            int result=enterpriseDicService.relationDicAndEnter(relation.getEnterId(),relation.getDicId());
            if(result>0){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }
        }catch (Exception e){
            returnObject.setMessage(Contents.RETURN_MESSAGE_EXCEPTION);
        }
        return returnObject;
    }

    @PostMapping("/relieve")
    public ReturnObject relieveRelationDicAndEnter(@RequestBody EnterpriseDicRelation relation){
        ReturnObject returnObject=new ReturnObject();
        returnObject.setCode(Contents.RETURN_CODE_FAIL);
        try {
            int result=enterpriseDicService.relieveRelationDicAndEnter(relation.getEnterId(),relation.getDicId());
            if(result>0){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }
        }catch (Exception e){
            returnObject.setMessage(Contents.RETURN_MESSAGE_EXCEPTION);
        }
        return returnObject;
    }
}
